package concurrent.masterandworker;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;

import java.util.concurrent.TimeUnit;

/**
 * @description: 测试类
 * @author: kanggw
 * @date: 2023/7/5
 **/
public class MasterWorkerTest {

    public static class SimpleTask extends Task<Integer> {


        @Override
        protected Integer doExecute() {
            System.out.println("task:" + getTaskId() + " is done");
            return getTaskId();
        }
    }

    public static void main(String[] args) {
        Master<SimpleTask, Integer> master = new Master<>(4);
        //定期向master提交任务
        ThreadUtil.schedule(null, () -> {
            master.submitMaster(new SimpleTask());
            System.out.println(DateUtil.date()+ "提交任务" );
        }, 2, 2, TimeUnit.SECONDS, true);
        //定期从master中取结果
        ThreadUtil.schedule(null, () -> {
            master.printResult();
            System.out.println(DateUtil.date() + "提取结果");
        }, 5, 5, TimeUnit.SECONDS, true);
    }


}
